'''
Created on Jan 9, 2010

@author: iGee
'''
from PIL import Image
import wx
class Convert:
    '''
    @staticmethod
    def bitmapToPil(bitmap):
        return Convert.imageToPil(Convert.bitmapToImage(bitmap))
    @staticmethod
    def bitmapToImage(bitmap):
        return wx.wxImageFromBitmap(bitmap)
    '''
    @staticmethod
    def pilToBitmap(pil):
        return Convert.imageToBitmap(Convert.pilToImage(pil,True))
    
    #Or, if you want to copy alpha channels too (available from wxPython 2.5)
    @staticmethod
    def pilToImage(pil,alpha=True):
        if alpha:
            image = wx.EmptyImage( *pil.size )
            image.SetData( pil.convert( "RGB").tostring() )
            image.SetAlphaData(pil.convert("RGBA").tostring()[3::4])
        else:
            image = wx.EmptyImage(pil.size[0], pil.size[1])
            new_image = pil.convert('RGB')
            data = new_image.tostring()
            image.SetData(data)
        return image
    
    @staticmethod
    def imageToPil(image):
        pil = Image.new('RGB', (image.GetWidth(), image.GetHeight()))
        pil.fromstring(image.GetData())
        return pil
    @staticmethod
    def imageToBitmap(image):
        return image.ConvertToBitmap()
    @staticmethod
    def getPixel(x,y,im):
        if 0<= x and x < im.size[0] and 0 <= y and y < im.size[1]:
            pix = im.load()
            return pix[x,y]
        else:
            return (0,0,0,0)